// Copyright Epic Games, Inc. All Rights Reserved.

#include "LyraWeaponUserInterface.h"

#include "Equipment/LyraEquipmentManagerComponent.h"
#include "GameFramework/Pawn.h"
#include "Weapons/LyraWeaponInstance.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraWeaponUserInterface)

struct FGeometry;

/**
 * 构造函数
 * @param ObjectInitializer 对象初始化器
 */
ULyraWeaponUserInterface::ULyraWeaponUserInterface(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)
{
}

/**
 * 本地构建
 */
void ULyraWeaponUserInterface::NativeConstruct()
{
	Super::NativeConstruct(); //调用父类本地构建
}

/**
 * 本地销毁
 */
void ULyraWeaponUserInterface::NativeDestruct()
{
	Super::NativeDestruct(); //调用父类本地销毁
}

/**
 * 本地Tick
 * @param MyGeometry 几何信息
 * @param InDeltaTime 增量时间
 */
void ULyraWeaponUserInterface::NativeTick(const FGeometry& MyGeometry, float InDeltaTime)
{
	Super::NativeTick(MyGeometry, InDeltaTime); //调用父类本地Tick

	if (APawn* Pawn = GetOwningPlayerPawn()) //如果拥有玩家Pawn存在
	{
		if (ULyraEquipmentManagerComponent* EquipmentManager = Pawn->FindComponentByClass<ULyraEquipmentManagerComponent>()) //如果找到装备管理器组件
		{
			if (ULyraWeaponInstance* NewInstance = EquipmentManager->GetFirstInstanceOfType<ULyraWeaponInstance>()) //如果获取到第一个武器实例
			{
				if (NewInstance != CurrentInstance && NewInstance->GetInstigator() != nullptr) //如果实例改变且新实例有发起者
				{
					ULyraWeaponInstance* OldWeapon = CurrentInstance; //保存旧武器
					CurrentInstance = NewInstance; //更新当前实例
					RebuildWidgetFromWeapon(); //从武器重新构建控件
					OnWeaponChanged(OldWeapon, CurrentInstance); //调用武器改变事件
				}
			}
		}
	}
}

/**
 * 从武器重新构建控件
 */
void ULyraWeaponUserInterface::RebuildWidgetFromWeapon()
{
	// 从武器重新构建控件的实现
}